package com.mz.servlet.sys;

import java.util.Date;
import java.util.Iterator;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.json.JSONArray;
import org.json.JSONObject;

import com.mz.base.util.ATTSystem;
import com.mz.base.util.Page;
import com.mz.entity.SysUsers;
import com.mz.service.sys.SysRoleService;
import com.mz.service.sys.SysUsersService;
import com.mz.servlet.CommonServlet;

public class SysUsersServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;

	public void doPost(HttpServletRequest request, HttpServletResponse response) {
		String operating = request.getParameter("operating");
		if(StringUtils.isEmpty(operating)){
			//查询管理员列表信息
			this.findSysUsers(request, response);
		}else if("loadData".equals(operating)){
			//初始化页面数据
			this.loadData(request, response);
		}else if("save".equals(operating)){
			//保存管理员信息
			this.saveSysUsers(request, response);
		}else if("get".equals(operating)){
			//获取管理员信息
			this.getSysUsersById(request, response);
		}else if("delete".equals(operating)){
			//删除管理员账号信息
			this.deleteSysUsers(request, response);
		}
	}
	/**
	 * 删除管理员账号信息
	 * @param request
	 * @param response
	 */
	private void deleteSysUsers(HttpServletRequest request, HttpServletResponse response) {
		String sUserId = request.getParameter("userId");
		Long userId = null;
		if(!StringUtils.isEmpty(sUserId)){
			userId = Long.valueOf(sUserId);
		}
		
		JSONObject retJson = new JSONObject();
		retJson.put("code", 0);
		if(userId==null){
			retJson.put("msg", "userId is null");
		}else{
			SysUsers sessionSysUser = (SysUsers) request.getSession().getAttribute(ATTSystem.KEY_SESSION_LOGIN);
			SysUsersService sysUsersService = new SysUsersService();
			int row = sysUsersService.deleteSysUsers(userId, sessionSysUser.getUserId());
			if(row <= 0){
				retJson.put("msg", "删除失败");
			}else{
				retJson.put("code", 1);
			}
		}
		
		CommonServlet.writer(response, retJson.toString());
	}

	/**
	 * 获取管理员信息
	 * @param request
	 * @param response
	 */
	private void getSysUsersById(HttpServletRequest request, HttpServletResponse response) {
		String sUserId = request.getParameter("userId");
		Long userId = null;
		if(!StringUtils.isEmpty(sUserId)){
			userId = Long.valueOf(sUserId);
		}
		
		JSONObject retJson = new JSONObject();
		retJson.put("code", 0);
		if(userId==null){
			retJson.put("msg", "userId is null");
		}else{
			SysUsersService sysUsersService = new SysUsersService();
			JSONObject jsonSysUsers = sysUsersService.getSysUsersById(userId);
			if(jsonSysUsers==null){
				retJson.put("msg", "no existent userId="+userId+" The row.");
			}else{
				retJson.put("code", 1);
				retJson.put("data", jsonSysUsers);
			}
		}
		
		CommonServlet.writer(response, retJson.toString());
	}

	/**
	 * 保存管理员信息
	 * @param request
	 * @param response
	 */
	private void saveSysUsers(HttpServletRequest request, HttpServletResponse response) {
		String sUserId = request.getParameter("userId");
		String userName = request.getParameter("userName");
		String realName = request.getParameter("realName");
		String password = request.getParameter("password");
		String sRoleId = request.getParameter("roleId");
		String sStatus = request.getParameter("status");
		String mobile = request.getParameter("mobile");
		String email = request.getParameter("email");
		
		Long userId = null, roleId = null;
		Integer status = null;
		if(!StringUtils.isEmpty(sUserId)){
			userId = Long.valueOf(sUserId);
		}
		if(!StringUtils.isEmpty(sRoleId)){
			roleId = Long.valueOf(sRoleId);
		}
		if(!StringUtils.isEmpty(sStatus)){
			status = Integer.valueOf(sStatus);
		}
		
		SysUsers sessionSysUser = (SysUsers) request.getSession().getAttribute(ATTSystem.KEY_SESSION_LOGIN);
		
		Date date = new Date();
		SysUsers sysUsers = new SysUsers(userId, userName, password, realName, status, email, mobile,
				roleId, date, sessionSysUser.getUserId(), date, sessionSysUser.getUserId());
		SysUsersService sysUsersService = new SysUsersService();
		int rows = sysUsersService.saveSysUsers(sysUsers);
		
		JSONObject retJson = new JSONObject();
		if(rows==1){
			retJson.put("code", 1);
		}else{
			retJson.put("code", 0);
			retJson.put("msg", "保存失败");
		}
		CommonServlet.writer(response, retJson.toString());
		
	}

	/**
	 * 初始化页面数据
	 * @param request
	 * @param response
	 */
	private void loadData(HttpServletRequest request, HttpServletResponse response) {
		SysRoleService sysRoleService = new SysRoleService();
		JSONArray roleJsonArray = sysRoleService.findSysRole();
		CommonServlet.writer(response, roleJsonArray.toString());
	}

	/**
	 * 查询管理员列表
	 * @param request
	 * @param response
	 */
	private void findSysUsers(HttpServletRequest request, HttpServletResponse response) {
		String sPageNo = request.getParameter("pageNo");
		String sPageSize = request.getParameter("pageSize");
		String userName = request.getParameter("userName");
		String sRoleId = request.getParameter("roleId");
		String sStatus = request.getParameter("status");
		
		Integer pageNo = 1, pageSize = 20, status = null;
		Long roleId = null;
		try {
			if(!StringUtils.isEmpty(sPageNo)){
				pageNo = Integer.valueOf(sPageNo);
			}
			if(!StringUtils.isEmpty(sPageSize)){
				pageSize = Integer.valueOf(sPageSize);
			}
			if(!StringUtils.isEmpty(sRoleId)){
				roleId = Long.valueOf(sRoleId);
			}
			if(!StringUtils.isEmpty(sStatus)){
				status = Integer.valueOf(sStatus);
			}
		} catch (Exception e) {}
		
		Page page = new Page(pageNo, pageSize);
		SysUsers sysUsers = new SysUsers();
		sysUsers.setUserName(userName);
		sysUsers.setRoleId(roleId);
		sysUsers.setStatus(status);
		
		SysUsersService sysUsersService = new SysUsersService();
		sysUsersService.findSysUsers(page, sysUsers);
		
		JSONObject jsonObject = new JSONObject();
		Iterator<Long> iterator = ATTSystem.MAP_SESSION_USERID.values().iterator();
		while(iterator.hasNext()){
			jsonObject.put("USERID_"+iterator.next(), true);
		}
		page.setOther(jsonObject);
		
		CommonServlet.writer(response, page.pageToJson());
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response) {
		this.doPost(request, response);
	}

	public void init() throws ServletException {}
	
	public SysUsersServlet() {
		super();
	}

	public void destroy() {
		super.destroy();
	}
	
}